﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Common;
using AspNetCs.Model;
using System.Data;
using System.Data.Sql;
using AspNetCs.Enumerations.Channel_Enum;
public partial class Users_memorial_List : BasePage
{
    protected string WhereStr = "";
    protected Memorial_Type MemorialTypeBll = new Memorial_Type();
    protected int IndexPage = 1;

    protected Memorial MemorialBll = new Memorial();

    protected void Page_Load(object sender, EventArgs e)
    {
        string s = (Request.Url.DnsSafeHost);

        if (Request["page"] != null)
        {
            IndexPage = StringHelper.isNumber_int(Request["page"].ToString()) == 0 ? 1 : StringHelper.isNumber_int(Request["page"].ToString());
        }

        Base_CurrPath = configInfo.SiteName + " >> 会员管理中心 >> 纪念馆列表";
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/memorial_list_" + IndexPage + ".Aspx", "", true);

        WhereStr = " isdel=0 and  CreateID=" + Base_GetLoginInfo.GetUserID.ToString();


        if (!Page.IsPostBack)
        {




            bind();
            strBuilder.AppendLine("<script>");
            strBuilder.AppendLine("function IndexOnTop(){");
            strBuilder.AppendLine("return confirm('是否要将该设为在线追思,这将扣除用户点数:" + configInfo.onLineLikePoint + "点\\r\\r开始时间:" + DateTime.Now.ToShortDateString() + "\\r\\r结束时间:" + DateTime.Now.AddMonths(configInfo.onlineTime).ToShortDateString() + "')");
            strBuilder.AppendLine("}");

            strBuilder.AppendLine("function Delinfo(){");
            strBuilder.AppendLine("return confirm('您是否真的删除此条纪念馆,这将同时删除与之相关的所有信息!')");
            strBuilder.AppendLine("}");





            strBuilder.AppendLine("</script>");
            Page.RegisterStartupScript("UserIndexOnTop", strBuilder.ToString());

        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {


        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (Repeater1.Items.Count == 0)//显示的数据项为0 既是没有数据
            {
                ((Panel)e.Item.FindControl("Panel_Noting")).Visible = true;

            }
        }

        if (e.Item.ItemIndex != -1)
        {

            LinkButton bt = e.Item.FindControl("btnCreate") as LinkButton;
            Label lb = e.Item.FindControl("Label1") as Label;
            DataRowView drv = (DataRowView)e.Item.DataItem;


            //string s = "开始时间:" + drv["IndexTop_BeginTime"].ToString() + "<br>结束时间:" + drv["IndexTop_EndTime"].ToString() + "";
            lb.Text = "<span id=\"IndexOnTop_id\">" + Base_FalseOrTrue(StringHelper.isNumber_int(drv["IsIndexTop"].ToString()), StringHelper.isDatetime(drv["IndexTop_BeginTime"].ToString()), StringHelper.isDatetime(drv["IndexTop_EndTime"].ToString())) + "</span>";

            if (StringHelper.isNumber_int(drv["IsIndexTop"].ToString()) == 1 || Utils.DateDiff(Utils.DateInterval.Minute, DateTime.Now, StringHelper.isDatetime(drv["IndexTop_EndTime"].ToString())) > 0)
            {
                bt.Enabled = false;
                bt.OnClientClick = "return false;";
                bt.Text = "已经申请";
            }
            if (Base_GetUserInfo.Point < configInfo.onLineLikePoint)
            {
                bt.Visible = false;
                bt.OnClientClick = "return false;";
            }



        }





    }

    /// <summary>
    /// 在线追思,返回"是"或者0返回"否"
    /// </summary>
    /// <param name="_Value"></param>
    /// <returns></returns>
    protected string Base_FalseOrTrue(int _Value, DateTime btime, DateTime dt)
    {
        string Result = "否";
        switch (_Value)
        {
            case 1:
                Result = "是";
                Result += "<br><span style='color:#ACA899;'>" + btime.ToShortDateString() + "<br>至<br>" + dt.ToShortDateString() + "</span>";
                break;
            default:
                Result = "否";
                break;
        }
        return Result;
    }



    protected void bind()
    {

        this.AspNetPager1.UrlRewritePattern = "memorial_List_{0}.Aspx";
        this.AspNetPager1.PageSize = 15;
        Pager p = new Pager();
        p.PageIndex = IndexPage;
        p.Ident = "id";
        p.Obj = Repeater1;
        p.Orderfld = "Addtime";
        p.OrderType = "desc";
        p.PageIndex = this.AspNetPager1.CurrentPageIndex;
        p.PageSize = this.AspNetPager1.PageSize;
        p.TableName = "memorial";
        p.WhereStr = WhereStr;
        p.ControlBind();
        this.AspNetPager1.RecordCount = p.recordcount();

    }

    protected string memorialType(Object _id)
    {
        string Result = "";
        try
        {
            Result = MemorialTypeBll.GetModel(StringHelper.isNumber_int(_id.ToString())).TypeName;
        }
        catch
        {
            Result = "";
        }
        return Result;
    }


    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {


        bind();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "CreateHtml")
        {
            memorial_model HTMLmd = Memorial_Bll.CreateNew.GetModel(StringHelper.isNumber_int(e.CommandArgument.ToString()));
            string NumUrl = HTMLmd.NumId + ".html";

            string CreateHtmlUrl = configInfo.InstallDir + "Html/" + Channel_Type.memorial + "/" + NumUrl;
            bool isok = TempleBll.CreateNew.TempleCreate(Server.MapPath(CreateHtmlUrl), NumUrl, Channel_Type.memorial, true);
            if (isok)
            {
                Response.Write("<script>alert('生成成功！');location.href='memorial_List_" + IndexPage + ".Aspx';</script>");
            }
            else
            {
                ResultStr("memorial_List_" + IndexPage + ".Aspx", "生成失败!");
            }
            Response.End();
        }
        else if (e.CommandName == "IndexOnTop")
        {

            //DataRowView drv = e.Item.DataItem as DataRowView;
            memorial_model md = new memorial_model();
            md.NumId = MemorialBll.GetModel(StringHelper.isNumber_int(e.CommandArgument.ToString())).NumId;
            md.IsIndexTop = 1;
            md.IndexTop_Begintime = DateTime.Now;
            md.IndexTop_Endtime = DateTime.Now.AddMonths(configInfo.onlineTime);
            md.CreateId = Base_GetLoginInfo.GetUserID;
            md.Hot = configInfo.onLineLikePoint;
            md.Id = StringHelper.isNumber_int(e.CommandArgument.ToString());
            MemorialBll.IndexOnTop(md);
            Response.Redirect("memorial_List_" + IndexPage + ".Aspx");
            Response.End();
        }
        if (e.CommandName == "delinfo")
        {
            MemorialBll.UserDel(" id=" + e.CommandArgument.ToString());
            Response.Redirect("memorial_List_" + IndexPage + ".Aspx");
            Response.End();
        }
    }
}
